﻿function Query(view, type) {
    require(["esri/tasks/QueryTask", "esri/tasks/support/Query", "esri/geometry/Point", "esri/geometry/geometryEngine", "esri/widgets/Popup"],
        function (QueryTask, Query, Point, geometryEngine, Popup) {
           
            view.on('click', function (evt) {
                var citiesLayerUrl = "";
                switch (type) {
                    case "sub":
                        citiesLayerUrl = "http://192.168.1.238:6080/arcgis/rest/services/现状电网/MapServer/3"; // Represents the REST endpoint for a layer of cities.
                        break;
                    case "line":
                        citiesLayerUrl = "http://192.168.1.238:6080/arcgis/rest/services/现状电网/MapServer/4";
                        break;
                }

                var queryTask = new QueryTask({
                    url: citiesLayerUrl
                });

                var point = new Point({
                    x: evt.mapPoint.x,
                    y: evt.mapPoint.y,
                    spatialReference: view.spatialReference
                });
                var ptBuff = geometryEngine.geodesicBuffer(point, 500, "meters");
                var query = new Query();
                query.returnGeometry = true;
                query.outFields = ["*"];
                //query.where = "ID>0";  // Return all cities with a population greater than 1 million
                query.geometry = ptBuff;
                query.outSpatialReference = view.spatialReference;

                // When resolved, returns features and graphics that satisfy the query.
                queryTask.execute(query).then(function (results) {
                    if (results.features != null && results.features.length > 0) {
                        var fc = results.features[0];
                        DrawQueryResult(fc.geometry, view);
                        var attrs = fc.attributes;
                        var table = "<table><tr><th>字段名</th><th>字段值</th></tr>";
                        var infos = "";
                        for (var d in attrs) {

                            var tr = "<tr><td>" + d + "</td><td>" + attrs[d] + "</td></tr>";
                            table += tr;
                        }
                        table += "</table>";

                        view.popup.open({
                            content: table,
                            //features: results.features,  // array of graphics
                            featureMenuOpen: true, // selected features initially display in a list
                            location: evt.mapPoint
                        }); 
                    }
                    else {
                        view.graphics.removeAll();
                    }
                });

                // When resolved, returns a count of the features that satisfy the query.
                queryTask.executeForCount(query).then(function (results) {
                    //alert(results);
                });
            })
        })
}

function DrawQueryResult(geo, view) {
    require(["esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol", "esri/Color", "esri/Graphic"],
        function (SimpleMarkerSymbol, SimpleLineSymbol, Color, Graphic) {
            view.graphics.removeAll();
            switch (geo.type) {
                case "point":
                    var graphic = new Graphic({
                        geometry: geo,
                        symbol: {
                            type: "simple-marker", // autocasts as SimpleMarkerSymbol
                            style: "circle",
                            color: [104, 254, 253],
                            size: "14px",
                            outline: {
                                width: 1,
                                color: [104, 254, 253]
                            }
                        }
                    });
                    view.graphics.add(graphic);
                    break;
                case "polyline":
                    var graphic = new Graphic({
                        geometry: geo,
                        symbol: {
                            type: "simple-line", // autocasts as SimpleLineSymbol
                            color: [104, 254, 253],
                            width: 3,
                            cap: "round",
                            join: "round"
                        }
                    });
                    view.graphics.add(graphic);
                    break;
            }
        })
}
